package com.shou.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shou.BusinessException;
import com.shou.CommonVar;
import com.shou.dao.CollectionDao;
import com.shou.dao.DistrictDao;
import com.shou.dao.ServeInfoDao;
import com.shou.dao.ServeTypeDao;
import com.shou.dao.support.Page;
import com.shou.entity.Collection;
import com.shou.entity.District;
import com.shou.entity.ServeInfo;
import com.shou.entity.ServeType;

@Service("serveInfoService")
public class ServeInfoService {
	
	@Autowired
	private ServeInfoDao serveInfoDaoImpl;
	
	@Autowired
	private ServeTypeDao serveTypeDaoImpl;
	
	@Autowired
	private CollectionDao collectionDaoImpl;
	
	@Autowired
	private DistrictDao districtDaoImpl;
	
	public Page<ServeInfo> getServePage(Integer pageNum,Integer pageSize,Long userId,ServeInfo serveInfoQuery) {
		
		if(pageNum == null) {
			pageNum = 1;
		}
		if(pageSize == null) {
			pageSize = CommonVar.PHONE_PAGESIZE;
		}
		Page<ServeInfo> page = serveInfoDaoImpl.queryPage(pageNum, pageSize, serveInfoQuery);
		if(userId != null) {
			Collection colQuery = new Collection();
			for(ServeInfo serveInfo : page.getRows()){
				colQuery.setServeId(serveInfo.getId());
				colQuery.setUserId(userId);
				List<Collection> colList = collectionDaoImpl.queryList(colQuery);
				if(colList.size() > 0) {
					
					serveInfo.setIsCollected(1);
				}
				else {
					serveInfo.setIsCollected(0);
				}				
				ServeType serveType = serveTypeDaoImpl.get(ServeType.class, serveInfo.getServeTypeId());
				serveInfo.setServeTypeColor(serveType.getTypeColor());
				serveInfo.setServeTypeName(serveType.getTypeName());
			}			
		}
		return serveInfoDaoImpl.queryPage(pageNum, pageSize, serveInfoQuery);
	}
	
	public ServeInfo getServeInfo(Long serveId){
		ServeInfo serveInfo = serveInfoDaoImpl.get(ServeInfo.class,serveId);
		ServeType serveType = serveTypeDaoImpl.get(ServeType.class, serveInfo.getServeTypeId());
		serveInfo.setServeTypeColor(serveType.getTypeColor());
		serveInfo.setServeTypeName(serveType.getTypeName());
		return serveInfo;
	}
	
	public ServeType getServeType(Integer serveTypeId) {
		return serveTypeDaoImpl.get(ServeType.class, serveTypeId);
	}
	
	public List<ServeType> getServeTypeList() {
		return serveTypeDaoImpl.getListBy(ServeType.class,"useStatus",CommonVar.USE_ONUSE);
	}
	
	public void saveServeType(ServeType serveType) {
		if(serveType.getId() == null) { //新增
			serveType.setCreateTime(new Date());
			serveType.setUseStatus(CommonVar.USE_ONUSE);
			serveTypeDaoImpl.saveOrUpdate(serveType);
		}
		else {
			ServeType oldType = serveTypeDaoImpl.get(ServeType.class,serveType.getId());
			oldType.setTypeName(serveType.getTypeName());
			oldType.setTypeColor(serveType.getTypeColor());
			oldType.setCategory(serveType.getCategory());
			serveTypeDaoImpl.saveOrUpdate(oldType);
		}
	}
	
	public void saveServeInfo(ServeInfo serveInfo){
		
		Integer districtId = serveInfo.getDistrictId();
		District district = districtDaoImpl.get(District.class,districtId);
		if(district !=null) {
			serveInfo.setDistrictName(district.getDistrict());
		}
		else {
			throw new BusinessException("invalid district");
		}
		serveInfo.setDistrictName(district.getDistrict());
		serveInfo.setCreateTime(new Date());
		serveInfo.setUseStatus(CommonVar.USE_ONUSE);
		serveInfo.setServeStatus(CommonVar.SERVE_STATUS_ON_SERVE);
		serveInfoDaoImpl.saveOrUpdate(serveInfo);
	}
	
	public List<District> getDistrictList() {
		return 	districtDaoImpl.getListBy(District.class,"useStatus",1);
	}
	
	public void finishServe(Long serveId) {
		ServeInfo serve = serveInfoDaoImpl.get(ServeInfo.class, serveId);
		serve.setServeStatus(CommonVar.SERVE_STATUS_FINISHED);
		serveInfoDaoImpl.saveOrUpdate(serve);
	}
}
